package Exception;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-11-11
 * Time: 16:10
 */
class Money implements Cloneable{
    public double money = 9.9;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Student implements Cloneable{
    public String name;
    public int age;
    public Money m;

    public Student(String name, int age,Money m) {
        this.name = name;
        this.age = age;
        this.m = m;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", money=" + m.money +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student newStudent = (Student)super.clone();
        newStudent.m = (Money) m.clone();
        return newStudent;
    }
}
public class Demo2 {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student stu1 = new Student("李四",19,new Money());
        Student stu2 = (Student)stu1.clone();
        System.out.println("stu1的money：" + stu1.m.money);
        System.out.println("修改stu2的money前：" + stu2.m.money);
        stu2.m.money = 99.99;
        System.out.println("修改stu2的money后：" + stu2.m.money);
        System.out.println("stu1的money：" + stu1.m.money);

    }
//    public static void main(String[] args) throws CloneNotSupportedException {
//        Student stu1 = new Student("lisi",19,new Money());
//        Student stu2 = (Student) stu1.clone();
//        System.out.println(stu1 == stu2);//判断两个对象是否一样
//    }
//
//    public static void main3(String[] args) {
//        String[] str = {"cd","ef","ab"};
//        System.out.println("排序前：" + Arrays.toString(str));
//        Arrays.sort(str);
//        System.out.println("排序后：" + Arrays.toString(str));
//    }
//    public static void main2(String[] args) {
//        //实例化三个学生
//        Student[] stu = new Student[3];
//        stu[0] = new Student("zhansan",19);
//        stu[1] = new Student("lisi",18);
//        stu[2] = new Student("wangwu",21);
//
//        //实例化比较器
//        ageComparator comparator1 = new ageComparator();
//        nameComparator comparator2 = new nameComparator();
//
//        System.out.println("使用age排序前：" + Arrays.toString(stu));
//        Arrays.sort(stu,comparator1);
//        System.out.println("使用age排序后：" + Arrays.toString(stu));
//        System.out.println();
//        System.out.println("使用name排序前：" + Arrays.toString(stu));
//        Arrays.sort(stu,comparator2);
//        System.out.println("使用name排序后：" + Arrays.toString(stu));
//    }
//
//    public static void main1(String[] args) {
//        Student stu1 = new Student("zhansan",19);
//        Student stu2 = new Student("lisi",20);
//
//        //实例化比较器
//        ageComparator comparator1 = new ageComparator();
//        nameComparator comparator2 = new nameComparator();
//
//        //根据年龄比较
//        int ret = comparator1.compare(stu1,stu2);//调用age比较器方法
//        if(ret == 0) System.out.println("stu1==stu2");
//        else if(ret < 0) System.out.println("stu1 < stu2");
//        else System.out.println("stu1 > stu2");
//
//        //根据名字比较
//        ret = comparator2.compare(stu1,stu2);//调用name比较器方法
//        if(ret == 0) System.out.println("stu1 == stu2");
//        else if(ret < 0) System.out.println("stu1 < stu2");
//        else System.out.println("stu1 > stu2");
//    }
}
